package com.zzy.mccjt.model;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.zzy.mccjt.constant.RequestConstant;
import lombok.Getter;
import org.slf4j.MDC;

/**
 * @program: mccjt-cloud
 * @description: 当前调用接口的客户端信息 - OpenAPI
 * @author: zhouyang.zheng
 * @create: 2022-08-02 18:25
 **/
@Getter
public class CurrentClient {

    private final String key;

    private static final TransmittableThreadLocal<CurrentClient> CURRENT = new TransmittableThreadLocal<>();

    public static CurrentClient get() {
        return CURRENT.get();
    }

    public static void set(String key) {
        CURRENT.set(new CurrentClient(key));
        MDC.put(RequestConstant.CLIENT_KEY, key);
    }

    public static void clear() {
        CURRENT.remove();
        MDC.remove(RequestConstant.CLIENT_KEY);
    }

    private CurrentClient(String key) {
        this.key = key;
    }
}
